package com.hoshiibuy.customerOperation.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiibuy.common.aop.Logable;
import com.hoshiibuy.common.exception.CommonError;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.customerOperation.dao.CartMapper;
import com.hoshiibuy.customerOperation.entity.Cart;
import com.hoshiibuy.customerOperation.feign.IdGeneratorService;
import com.hoshiibuy.customerOperation.service.ICartService;

/**
 * <p>
 * 购物车购物信息记录表 服务实现类
 * </p>
 *
 * @author Mr.Wu
 * @since 2019-04-28
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {

	@Autowired
	private CartMapper cartMapper;

	@Autowired
	private IdGeneratorService idGeneratorService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "Cart" }, allEntries = true)
	@Logable(authName = "前台用户操作-购物车购物信息记录表信息管理-新增购物车购物信息记录表信息")
	public Response insert(Cart param) {
		String id = idGeneratorService.queryGeneratorId();
		if (StringUtils.isBlank(id)) {
			throw new ServiceException(CommonError.error("id服务失败"));
		}
		// TODO 判断用户是否存在
		// TODO 判断商品是否存在
		// TODO 获取商品属性

		param.setRecId(Long.parseLong(id));
		param.setAddedtime(LocalDateTime.now());
		int count = cartMapper.insert(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("新增失败"));
		}
		return Response.success("新增成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "Cart" }, allEntries = true)
	@Logable(authName = "前台用户操作-购物车购物信息记录表信息管理-修改购物车购物信息记录表信息")
	public Response updateDataById(Cart param) {
		// 前台修改数量,当商品信息发生变动时应修改为失效状态
		int count = cartMapper.updateById(param);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("修改失败"));
		}
		return Response.success("修改成功");
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "Cart" }, allEntries = true)
	@Logable(authName = "前台用户操作-购物车购物信息记录表信息管理-删除购物车购物信息记录表信息")
	public Response deleteDataById(String id) {
		int count = cartMapper.deleteById(id);
		if (count <= 0) {
			throw new ServiceException(CommonError.error("删除失败"));
		} else {
			return Response.success("删除成功");
		}
	}

	@Override
	@Cacheable(cacheNames = "Cart", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
	@Logable(authName = "前台用户操作-购物车购物信息记录表信息管理-查询购物车购物信息记录表信息详情")
	public Response getDetail(String id) {
		Cart data = cartMapper.selectById(id);
		if (data == null) {
			return Response.failed("数据不存在");
		} else {
			return Response.data(data);
		}
	}

	@Override
	@Cacheable(cacheNames = "Cart", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5(Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "前台用户操作-购物车购物信息记录表信息管理-查询购物车购物信息记录表信息列表")
	public Response getList(QueryWrapper<Cart> queryWrapper) {
		return Response.data(cartMapper.selectList(queryWrapper));
	}

	@Override
	@Cacheable(cacheNames = "Cart", key = "T(com.hoshiibuy.common.utils.DigestUtils).md5('Page(current=' + #page.getCurrent() + ', size=' + #page.getSize() + '), Wrapper(SqlSelect=' + #queryWrapper.getSqlSelect() + ', SqlSegment=' + #queryWrapper.getSqlSegment() + ')')")
	@Logable(authName = "前台用户操作-购物车购物信息记录表信息管理-分页查询购物车购物信息记录表信息")
	public Response getPage(IPage<Cart> page, QueryWrapper<Cart> queryWrapper) {
		return Response.data(cartMapper.selectPage(page, queryWrapper));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@CacheEvict(cacheNames = { "Cart" }, allEntries = true)
	@Logable(authName = "前台用户操作-购物车购物信息记录表信息管理-更新购物车购物信息有效信息")
	public Response updateCartInfo(QueryWrapper<Cart> queryWrapper) {
		// TODO 获取用户购物车列表
		List<Cart> cartList = cartMapper.selectList(queryWrapper);
		// 组装商品ID列表
		List<Long> idList = new ArrayList<Long>();
		for (Cart cart : cartList) {
			idList.add(cart.getRecId());
		}
		// TODO 根据ID列表获取商品信息
		// TODO 商品信息和购物车比对,如果价格不同，更新购物车为失效
		return Response.success("更新成功");
	}
}
